home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ln / ln.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-31  |  3.8 KB  |  184 lines

  1. /*
  2.  * Copyright (c) 1987 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific written prior permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifndef lint
  14. char copyright[] =
  15. "@(#) Copyright (c) 1987 Regents of the University of California.\n\
  16.  All rights reserved.\n";
  17. #endif /* not lint */
  18.  
  19. #ifndef lint
  20. static char sccsid[] = "@(#)ln.c    4.10 (Berkeley) 11/30/87";
  21. #endif /* not lint */
  22.  
  23. #include <sys/param.h>
  24. #include <sys/stat.h>
  25. #include <stdio.h>
  26. #include <errno.h>
  27.  
  28. #include <sprite.h>
  29. #include <fs.h>
  30. #include <status.h>
  31.  
  32. static int    fflag,                /* undocumented force flag */
  33.         sflag,                /* symbolic, not hard, link */
  34.         rflag,                /* remote link for Sprite */
  35.         (*linkf)();            /* system link call */
  36.  
  37. /*
  38.  * Forward declarations to keep gcc happy:
  39.  */
  40.  
  41. static int linkit(), usage(), remoteLink();
  42.  
  43. main(argc, argv)
  44.     int    argc;
  45.     char    **argv;
  46. {
  47.     extern int    optind;
  48.     struct stat    buf;
  49.     int    ch, exitval, link(), symlink();
  50.     char    *sourcedir;
  51.  
  52.     while ((ch = getopt(argc, argv, "fsr")) != EOF)
  53.         switch((char)ch) {
  54.         case 'f':
  55.             fflag = 1;
  56.             break;
  57.         case 's':
  58.             sflag = 1;
  59.             break;
  60.         case 'r':
  61.             rflag = 1;
  62.             break;
  63.         case '?':
  64.         default:
  65.             usage();
  66.         }
  67.  
  68.     argv += optind;
  69.     argc -= optind;
  70.  
  71.     if (rflag && sflag) {
  72.         usage();
  73.     }
  74.  
  75.     if (sflag) {
  76.         linkf = symlink;
  77.     } else if (rflag) {
  78.         linkf = remoteLink;
  79.     } else {
  80.         linkf = link;
  81.     }
  82.  
  83.     switch(argc) {
  84.     case 0:
  85.         usage();
  86.     case 1:                /* ln target */
  87.         exit(linkit(argv[0], ".", 1));
  88.     case 2:                /* ln target source */
  89.         exit(linkit(argv[0], argv[1], 0));
  90.     default:            /* ln target1 target2 directory */
  91.         sourcedir = argv[argc - 1];
  92.         if (stat(sourcedir, &buf)) {
  93.             perror(sourcedir);
  94.             exit(1);
  95.         }
  96.         if ((buf.st_mode & S_IFMT) != S_IFDIR)
  97.             usage();
  98.         for (exitval = 0; *argv != sourcedir; ++argv)
  99.             exitval |= linkit(*argv, sourcedir, 1);
  100.         exit(exitval);
  101.     }
  102.     /*NOTREACHED*/
  103. }
  104.  
  105. static
  106.  linkit(target, source, isdir)
  107.     char    *target, *source;
  108.     int    isdir;
  109. {
  110.     extern int    errno;
  111.     struct stat    buf;
  112.     char    path[MAXPATHLEN],
  113.         *cp, *rindex(), *strcpy();
  114.  
  115.     if (!sflag && !rflag) {
  116.         /* if target doesn't exist, quit now */
  117.         if (stat(target, &buf)) {
  118.             perror(target);
  119.             return(1);
  120.         }
  121.         /* only symbolic links to directories, unless -f option used */
  122.         if (!fflag && (buf.st_mode & S_IFMT) == S_IFDIR) {
  123.             printf("%s is a directory.\n", target);
  124.             return(1);
  125.         }
  126.     }
  127.  
  128.     /* if the source is a directory, append the target's name */
  129.     if (isdir || !stat(source, &buf) && (buf.st_mode & S_IFMT) == S_IFDIR) {
  130.         if (!(cp = rindex(target, '/')))
  131.             cp = target;
  132.         else
  133.             ++cp;
  134.         (void)sprintf(path, "%s/%s", source, cp);
  135.         source = path;
  136.     }
  137.  
  138.     if ((*linkf)(target, source)) {
  139.         perror(source);
  140.         return(1);
  141.     }
  142.     return(0);
  143. }
  144.  
  145. static
  146. remoteLink(target, source)
  147.     char *target;
  148.     char *source;
  149. {
  150.     struct stat stb;
  151.     ReturnStatus status;
  152.  
  153.     if (target[0] != '/') {
  154.     fprintf(stderr, "Target \"%s\" should be an absolute path\n", target);
  155.     return(1);
  156.     }
  157.     if (lstat(source, &stb) >= 0) {
  158.     /*
  159.      * The link already exists.  Rewrite it if it's a remote link,
  160.      * otherwise complain.
  161.      */
  162.     if ((stb.st_mode&S_IFMT) != S_IFRLNK) {
  163.         fprintf(stderr, "\"%s\" exists\n",source);
  164.         return(1);
  165.     } else if (unlink(source) < 0) {
  166.         perror(target);
  167.         return(1);
  168.     }
  169.     }
  170.     status = Fs_SymLink(target, source, 1);
  171.     if (status != 0) {
  172.     Stat_PrintMsg(status, "Fs_SymLink");
  173.     return(1);
  174.     }
  175.     return(0);
  176. }
  177.  
  178. static
  179. usage()
  180. {
  181.     fputs("usage:\tln [-s | -r] targetname [sourcename]\n\tln [-s | -r] targetname1 targetname2 [... targetnameN] sourcedirectory\n\n", stderr);
  182.     exit(1);
  183. }
  184.